package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.BusinessException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import groovy.util.logging.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author feihao
 * @version 1.0
 * @date 2021/7/21 19:01
 */

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {

        //参数检测：
        if (channelDto == null) {
            //直接返回无效参数对象：
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查页码参数
        channelDto.checkParam();
        PageResponseResult pageResponseResult = null;


        try {
            //按照名称模糊查询：
            QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
            Page<AdChannel> page = new Page(channelDto.getPage(), channelDto.getSize());
            //模糊查询字段不为空：
            queryWrapper.like(StringUtils.isNotBlank(channelDto.getName()), "name", channelDto.getName());
            queryWrapper.eq(channelDto.getStatus() != null, "status", channelDto.getStatus());
            IPage<AdChannel> iPage = adChannelMapper.selectPage(page, queryWrapper);

            List list = iPage.getRecords();

            //3.结果封装
            pageResponseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) iPage.getTotal());
            pageResponseResult.setData(list);
        } catch (Exception e) {
            //出错后抛出自定义异常，通用异常类拦截进行返回前端友好提示。
            throw new BusinessException();
        }

        return pageResponseResult;
    }


    /**
     * 新增频道信息
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannel.setCreatedTime(new Date());
        try {
            save(adChannel);
        } catch (Exception e) {
            throw new BusinessException();
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        try {
            updateById(adChannel);
        } catch (Exception e) {
            throw new BusinessException();
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据ID删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {

        //检查参数：
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //首先判断频道是否存在：
        AdChannel channel = getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断status是否有效
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道有效不能删除");
        }
        try {
            removeById(id);
        } catch (Exception e) {
            throw new BusinessException();
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
